﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebPartPages;
using System.IdentityModel.Tokens;
using System.Web.UI.WebControls;
using Microsoft.IdentityModel.Protocols.WSTrust;
using System.ServiceModel;
using Veracruz.IdentityModel;

namespace Veracruz.Facebook.Sharepoint.Webparts
{
    public class MyFacebookFriends
    : WebPart
    {
        #region fields
        private const string NotLoggedIn = "You are not logged in.";
        #endregion

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            SecurityToken __token = Page.Session["cachedToken"] as SecurityToken;

            if (__token == null)
            {
                Label __noFriends = new Label();
                __noFriends.Text = NotLoggedIn;
                __noFriends.RenderControl(writer);
            }
            else
            {
                ChannelFactory<ISocialNetworkService> l_channelFactory = new ChannelFactory<ISocialNetworkService>("FacebookService");
                //WSTrustClientCredentials clientCreds;
                FederatedClientCredentials.ConfigureChannelFactory<ISocialNetworkService>(l_channelFactory);
                //ISocialNetworkService l_service = WSTrustClientCredentials.ConfigureChannelFactory<ISocialNetworkService>(l_channelFactory, out clientCreds);
                //clientCreds.ActAs = __token;
                ISocialNetworkService l_service = l_channelFactory.CreateChannelActingAs<ISocialNetworkService>(__token);

                UserInfo[] l_friends = l_service.GetFriendFullDetails("uid");

                writer.WriteLine("<table>");
        
                foreach (UserInfo userInfo in l_friends)
	            {
                    writer.WriteLine("<tr>");
                    writer.WriteLine("<td align=center valign=middle>");

            		Image l_image = new Image();
                    l_image.ImageUrl = userInfo.SmallPictureUrl;
                    l_image.AlternateText = "Friend picture";
                    l_image.ToolTip = String.Format("{0} {1} - {2}", userInfo.FirstName, userInfo.LastName, userInfo.UserId);
                    l_image.RenderControl(writer);
                    writer.WriteLine("</td>");
                    writer.WriteLine("<td align=center valign=middle>");
                    HyperLink l_friendLink = new HyperLink();
                    l_friendLink.Target = "_blank";
                    l_friendLink.Text = String.Concat(userInfo.FirstName, " ", userInfo.LastName);
                    l_friendLink.NavigateUrl = String.Format("http://www.facebook.com/profile.php?id={0}", userInfo.UserId);
                    l_friendLink.RenderControl(writer);
                    //Label l_label = new Label();
                    //l_label.Text = 
                    //l_label.RenderControl(writer);        
                    writer.WriteLine("</td>");
                    writer.WriteLine("</tr>");
	            }

                writer.WriteLine("</table>");

                l_channelFactory.Close();
            }

            base.Render(writer);
        }
    }
}
